<?php
defined('BASEPATH') OR exit('No direct script access allowed');
//账户登录
class Login extends MY_Controller {

	function __construct(){
		
		parent::__construct();
		
	}
	
	function CheckLogin(){
		
		return true;
	}
	
	//start
	/**
	* @name 获取验证码
	* @return 成功返回1
	*/
	//end
	function get_verycode(){
		
		
		$code = get_rand_id(12);
		
		require_once(APPPATH . 'libraries/Captcha.php');
		
		$dir = FCPATH . 'images/captcha/';
		$img = new Captcha($code,$dir, 100, 30);
		$this->load->model('User/ValidateRecordModel','ValidateRecordModel');
		
		ob_start();
		
		$img->generate_image(false,function($mcode,$word){
			$expired_time = time() + 30*60;
			$this->ValidateRecordModel->save_validate_record($mcode,$word,'very_code',0,$expired_time);
		});
		
		$image_data = ob_get_contents();
		
		ob_end_clean();
		$url = 'data:image/png;base64,' . chunk_split(base64_encode($image_data));
		
		
		$arr = array(
			'code'=>$code,
			'img'=>$url
		);
		
		$this->data_out($arr);
		
	}
	//start
	/**
	* @name 登录
	* @param username ture char 账户
	* @param password ture char 密码
	* @param verf_token ture char 验证码key
	* @param verf_code ture char 验证码
	* @return 成功返回1
	*/
	//end
	function login(){
		
		$username	= $this->input->post('username',null,'');
		$password	= $this->input->post('password',null,'');
		$verf_token	= $this->input->post('verf_token',null,'');
		$verf_code	= $this->input->post('verf_code',null,'');
		
		if(empty($username)){
			$this->err_out("请输入用户名称");
		}
		if(empty($password)){
			$this->err_out("请输入登录密码");
		}
		
		$this->load->model('User/ValidateRecordModel','ValidateRecordModel');
		$result = $this->ValidateRecordModel->validate_code($verf_token,$verf_code,'very_code',true);
		if($result!==true){
			$this->err_out("验证码错误");
		}
		
		$this->load->model('User/AdminModel','AdminModel');
		
		
		
		$info = $this->AdminModel->field_get_info('username',$username);
		if(!$info){
			$this->err_out("用户名或密码不正确");
		}

		if($info['password']!==md5($password)){
			$this->err_out("用户名或密码不正确");
		}
		
		$this->ValidateRecordModel->remove_validate_record($verf_token,'very_code');
		
		
		
		$token = $this->AdminModel->get_token($info);
		$this->admin = $info;
		$this->user_id = $info['id'];
		

		
		
		//获取所属公司
		$this->load->model('User/companyModel','companyModel');
		$company = $this->companyModel->name_get_data($info['company']);
		
		
		//添加缓存开始，ling添加，20191012
		if(file_exists(APPPATH."cache/all_priv_cache.php")){
			include(APPPATH."cache/all_priv_cache.php");

			if($priv_cache){
				$res = $priv_cache;
			}
		}else{
			$this->load->dmodel('priv_group','priv_group');
			$res = $this->priv_group->getAll(array('is_enable'=>1));
		}
		
		
		
		//获取权限组的权限
		$privs = array();
		$querys = array();
		$actions = array();
		
		if($company){
			$querys[] = $company['id'];
		}
		
		
		foreach($res as $val){
			$ls_ids = explode(',',$val['ids']);
			if(in_array($this->user_id,$ls_ids)){
				$ls_privs = explode(',',$val['privs']);
				$privs = array_merge($privs,$ls_privs);
				
				$ls_querys = explode(',',$val['querys']);
				$querys = array_merge($querys,$ls_querys);
				
				$ls_actions = explode(',',$val['actions']);
				$actions = array_merge($actions,$ls_actions);
			}
		}
		$privs = array_unique($privs);
		$this->privs = implode(',',$privs);
		
		
		$querys = array_unique($querys);
		$this->querys = implode(',',$querys);
		
		$actions = array_unique($actions);
		$this->actions = implode(',',$actions);
		
		if(!$this->check_priv('role')){
			$this->load->model('Index/ConfigModel','ConfigModel');
			$sys_status = $this->ConfigModel->get_config('sys_status');
			if($sys_status==0){
				$reason = $this->ConfigModel->get_config('reason');
				$this->err_out($reason,404);
			}
		}
		
		
		
		$info = $this->data_filter($info,null,array('password','openid'));
		
		$arr = array(
			'token'=>$token,
			'info'=>$info
		);
		
		$ip = getIp();
		$device = getBrowser();
		$time = time();
		$region = '';
		$result = ip_get_City($ip);
		
		
		if($result && $result['code']==0){
			$region = $result['data']['region'].$result['data']['city'];
		}
		
		
		$this->load->dmodel('user_login_log','user_login_log');
		$log_arr = array(
			'user_id'=>$this->user_id,
			'addtime'=>$time,
			'ip'=>$ip,
			'region'=>$region,
			'device'=>$device
		);
		$this->user_login_log->add($log_arr);
		
		
		$this->add_log("员工【{$username}】登录进来",'admin');

		//在这里查询系统允许的最大提醒金额,ling添加
		$this->load->model('Index/ConfigModel','ConfigModel');
		$arr['max_money'] = $this->ConfigModel->get_config('max_money');
		

		$this->data_out($arr);
	}
	
	function get_config(){
		$this->load->model('Index/ConfigModel','ConfigModel');
		$info = array();
		
		$sys_name = $this->ConfigModel->get_config('sys_name');
		$info['sys_name'] = $sys_name;
		
		$sys_logo = $this->ConfigModel->get_config('sys_logo');
		$info['sys_logo'] = $sys_logo;
		
		
		$this->data_out($info);
	}
}
